package com.asset.user.biz.impl;


import com.asset.common.enums.ResultCodeEnum;
import com.asset.common.exception.BizException;
import com.asset.common.result.ResultMsg;
import com.asset.common.utils.StringUtil;
import com.asset.common.utils.ValidUtils;
import com.asset.user.biz.IUserBiz;
import com.asset.user.dao.entity.UserEntity;
import com.asset.user.dao.mapper.IUserMapper;
import com.asset.user.form.UserForm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class UserBizImpl implements IUserBiz {

    @Resource
    private IUserMapper userMapper;

    @Override
    public ResultMsg addUser(UserForm userForm) {
        return null;
    }

    @Override
    public UserForm getUserById(Long userId) {
        if (ValidUtils.isInvalid(userId)) {
            return null;
        }
        UserEntity userEntity = userMapper.getUserById(userId);
        return UserForm.convert2Form(userEntity);
    }

    @Override
    public ResultMsg update(UserForm userForm) {
        ResultMsg resultMsg = new ResultMsg();
        if (userForm == null || userForm.getId() == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("参数异常");
            return resultMsg;
        }
        try {
            UserEntity userEntity = UserForm.convert2Entity(userForm);
//            userDao.update(userEntity);
        } catch (Exception e) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("修改失败！请重试");
            return resultMsg;
        }
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        resultMsg.setMsg("修改成功！");
        return resultMsg;
    }

    @Override
    public UserForm getUserInfo() {
        return null;
    }

    @Override
    public UserEntity getUserByTel(String tel) {
        if (StringUtil.isBlank(tel)) {
            return null;
        }
        return userMapper.getUserByTel(tel);
    }

    @Override
    public void register(UserEntity userEntity) throws BizException {
        userMapper.register(userEntity);
    }

    @Override
    public List<UserEntity> getUsersByIds(List<Long> userIds) {
        return userMapper.getUsersByIds(userIds);
    }

}
